import re


# 恢复转义
def unescape(s):
    output_string = ""
    i = 0
    s = s[2:-2]
    while i < len(s):
        if s[i:i + 4] == "7D02":
            output_string += "7E"
            i += 4
        elif s[i:i + 4] == "7D01":
            output_string += "7D"
            i += 4
        else:
            output_string += s[i:i + 4]
            i += 4
    return '7E' + output_string + '7E'


# 自动去除报文中不合法字符，规范报文
def filter_non_hex_chars(input_str):
    # 使用正则表达式匹配0-F的字符，过滤掉其他字符
    filtered_str = re.sub(r'[^0-9A-Fa-f]', '', input_str)
    return filtered_str
